<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Muse","version":"7.7.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="title: python切片 date: 2020-4-4 20:00 切片 （slice）作用：从一个字符串序列中取出相应的元素重新组成一个字符串序列  语法：字符串序列[(开始索引b):(结束索引e)(:(步长s))]（注：（）内括起的部分代表可以省略）   说明:    1.　开始索引是切片开始切下的位置，0代表第一个元素，1代表第二个元素,-1代表最后一个  结束索引是切片的终止索引(但">
<meta property="og:type" content="article">
<meta property="og:title" content="python切片">
<meta property="og:url" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/index.html">
<meta property="og:site_name" content="tender healer">
<meta property="og:description" content="title: python切片 date: 2020-4-4 20:00 切片 （slice）作用：从一个字符串序列中取出相应的元素重新组成一个字符串序列  语法：字符串序列[(开始索引b):(结束索引e)(:(步长s))]（注：（）内括起的部分代表可以省略）   说明:    1.　开始索引是切片开始切下的位置，0代表第一个元素，1代表第二个元素,-1代表最后一个  结束索引是切片的终止索引(但">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404210132444.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404210224781.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404220714991.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221030561.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221116556.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221239640.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221351907.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404202733561.png">
<meta property="og:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404222625266.png">
<meta property="article:published_time" content="2020-04-04T14:34:43.517Z">
<meta property="article:modified_time" content="2020-04-04T14:39:21.295Z">
<meta property="article:author" content="YQ Cong">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/image-20200404210132444.png">

<link rel="canonical" href="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true
  };
</script>

  <title>python切片 | tender healer</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">tender healer</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">Recording learning gains</p>
  </div>

  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/04/python%E5%88%87%E7%89%87/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="YQ Cong">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="tender healer">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          python切片
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-04-04 22:34:43 / 修改时间：22:39:21" itemprop="dateCreated datePublished" datetime="2020-04-04T22:34:43+08:00">2020-04-04</time>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>title: python切片</p>
<p>date: 2020-4-4 20:00</p>
<p><strong>切片</strong> （slice）作用：从一个字符串序列中取出相应的元素重新组成一个字符串序列</p>
<p> 语法：字符串序列[(开始索引b):(结束索引e)(:(步长s))]（注：（）内括起的部分代表可以省略）</p>
<p>  说明:</p>
<p>   1.　开始索引是切片开始切下的位置，0代表第一个元素，1代表第二个元素,-1代表最后一个</p>
<ol start="2">
<li><p>结束索引是切片的终止索引(但不包含终止点)</p>
</li>
<li><p>步长是切片每次获取完当前元素后移动的方向和偏移量（  步长代表一下次索引的取值的增长值）</p>
</li>
</ol>
<p>​     1)没有步长，相当于取值完成后向右移动一个索引的位置(默认为1)</p>
<p>​     2) 当步长为正数时，取正向切片:</p>
<p>​      <strong>步长默认为1,开始索引默认为0,结束索引默认为len(s)</strong></p>
<p>​    3) 当步长为负整数时，取反向切片：此时，默认的起始位置是最后一个元素，终止位置是第一个元素的前一个位置</p>
<p>例1：定义一个变量S，并给变量S赋值为’ABCDE’,从索引为1的地方开始切，切到索引4，用一个变量a接收并打印结果，代码如下图：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404210132444.png" alt="image-20200404210132444"></p>
<p>例2：从倒数第二个索引开始切数，切到倒数第1个。代码如下：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404210224781.png" alt="image-20200404210224781"> </p>
<p>例3：从第一个开始切，结束索引是1000，代码如下：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404220714991.png" alt="image-20200404220714991"></p>
<p>例4：定义一个变量S，并给变量S赋值，从第一个切到第四个，步长为2，代码如下图：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221030561.png" alt="image-20200404221030561"></p>
<p>例5：定义一个变量，从最后一个位置开始切，切到终止位置，代码如下图所示：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221116556.png" alt="image-20200404221116556"></p>
<p>练习：1、写一个程序，输入一个字符串，把字符串的第一个字符和最后一个字符去掉，打印出处理后的字符串：代码如下：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221239640.png" alt="image-20200404221239640"></p>
<p>2、  输入一个字符串，判断这个字符串是否是回文（回文是指中心对称的文字）如：（上海自来水来自海上）代码如下：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404221351907.png" alt="image-20200404221351907"></p>
<p>在kali linux终端用python实例如下：</p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404202733561.png" alt="image-20200404202733561"></p>
<p><img src="/2020/04/04/python%E5%88%87%E7%89%87/image-20200404222625266.png" alt="image-20200404222625266"></p>
<p>粉色部分为输入部分</p>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/02/%E5%85%B3%E4%BA%8Ebinwalk/" rel="prev" title="关于binwalk">
      <i class="fa fa-chevron-left"></i> 关于binwalk
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/05/%E7%AC%94%E8%AE%B0%E6%95%B4%E7%90%86%EF%BC%88%E6%97%A0%E5%88%86%E7%B1%BB%EF%BC%89/" rel="next" title="笔记整理（无分类）">
      笔记整理（无分类） <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">YQ Cong</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">13</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">YQ Cong</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v4.2.0
  </div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.7.1
  </div>

        








      </div>
    </footer>
  </div>

  
  
  <script color='0,0,255' opacity='0.5' zIndex='-1' count='99' src="/lib/canvas-nest/canvas-nest.min.js"></script>
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
